﻿@model LanLordz.Models.Venue
@{
    var controller = this.ViewContext.Controller as LanLordzBaseController;
    ViewBag.Title = Model == null ? "Create Venue" : "Edit Venue";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Address)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Address)
        @Html.ValidationMessageFor(model => model.Address)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Latitude)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.Latitude)
        @Html.ValidationMessageFor(model => model.Latitude)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Longitude)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.Longitude)
        @Html.ValidationMessageFor(model => model.Longitude)
    </div>

    <input type="submit" value="Save" />
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
    $(function () {
        var geocoder = new google.maps.Geocoder();

        var addrToken = 0;
        $("#Address").change(function () {
            addrToken += 1;
            var requestToken = addrToken;
            $('#Latitude').val('');
            $('#Longitude').val('');

            geocoder.geocode({ 'address': $(this).val() }, function (results, status) {
                if (addrToken == requestToken && status == google.maps.GeocoderStatus.OK) {
                    var loc = results[0].geometry.location;
                    $('#Latitude').val(loc.lat());
                    $('#Longitude').val(loc.lng());
                }
            });
        });
    });
</script>
